import { Menu } from "@/common/model/app/menu";
import { type TabPaneName } from "element-plus";

export default function() {

    //region # 字段

    // 应用实例
    const { proxy }:any = getCurrentInstance()

    // 首页菜单
    let homeMenu:Menu;

    // 菜单列表
    const menus = reactive(new Array<Menu>())

    // 活跃菜单列表
    const activeMenus:Set<Menu> = reactive(new Set<Menu>())

    // 当前菜单标识
    const currentMenuIndex = ref('0')

    //endregion

    //region # 方法

    /**
     * 菜单导航
     * @param menu - 菜单
     * */
    const navigateTo = function(menu:Menu) {

        if (menu.routerPath == "")
            return;

        if (!activeMenus.has(menu)) {
            activeMenus.add(menu);

            proxy.$storage.pinia.keepAlive.includes.push(menu.componentName)
        }

        currentMenuIndex.value = menu.index;

        proxy.$router.push(menu.routerPath)
    }

    /**
     * 菜单标签变更
     * @param tabName - 标签名称
     * */
    const menuTabChanged = function(tabName:TabPaneName):void {

        currentMenuIndex.value = tabName.toString()

        let menu:Menu | null = getActiveMenu(currentMenuIndex.value);
        activeMenus.forEach((item:Menu) => {
            if (item.index == currentMenuIndex.value) {
                menu = item;
                return;
            }
        })
        menu = menu == null ? homeMenu : menu;

        navigateTo(menu)
    }

    /**
     * 吊销菜单
     * @param tabName -标签名称
     * */
    const deactiveMenu = function(tabName:TabPaneName):void {

        const tabNameStr:string = tabName.toString();
        const menu:Menu | null = getActiveMenu(tabNameStr)

        if (menu == null)
            return;

        let lastMenu:Menu | null = null;
        for (const activeMenu of activeMenus) {
            if (activeMenu.index == tabNameStr)
                break;

            lastMenu = activeMenu;
        }

        activeMenus.delete(menu)
        proxy.$storage.pinia.keepAlive.removeInclude(menu.componentName)

        if (lastMenu == null || currentMenuIndex.value != tabNameStr)
            return;

        let { index } = lastMenu;
        menuTabChanged(index);
    }

    /**
     * 初始化菜单
     * */
    const initMenu = function() {

        menus.push(new Menu("1", "菜单样例", false, 1, "menu", "", "", [
            new Menu("1-1", "子菜单01", true, 1, "menu", "", ""),
            new Menu("1-2", "子菜单02", true, 2, "", "", "")
        ]))

        menus.push(new Menu("2", "功能样例", false, 1, "menu", "", "", [
            new Menu("2-1", "UI 样例", true, 1, "ElementPlus", "DemoListView", "/demoList"),
            new Menu("2-2", "图表样例", true, 2, "PieChart", "DemoEchartsView", "/demoEcharts")
        ]))
    }

    /**
     * 初始化首页菜单
     * */
    const initHomeActiveMenu = function() {
        homeMenu = new Menu("0", "首页", false, 0, "", "home", "/home", [])
        activeMenus.add(homeMenu);
        navigateTo(homeMenu)
    }

    /**
     * 获取活跃菜单
     * @param menuIndex - 菜单索引
     * */
    const getActiveMenu = function(menuIndex:string):Menu | null {

        let menu:Menu | null = null;
        for (const activeMenu of activeMenus) {
            if (activeMenu.index == menuIndex) {

                menu = activeMenu;
                break;
            }
        }

        return menu;
    }

    //endregion

    return {
        menus,
        activeMenus,
        currentMenuIndex,

        initMenu,
        initHomeActiveMenu,
        navigateTo,
        menuTabChanged,
        deactiveMenu,
    }
}